﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using SBLib.Common.CsvKS;
using VicMessenger.DB;
using VicMessenger.Common;
using SBLib.Common.SqlServerKS;
using VicMessenger.BLL;


namespace VicMessenger
{
    /// <summary>
    /// Interaction logic for SourceSelection.xaml
    /// </summary>
    public partial class SourceSelection : Window
    {
        SqlServerFuncKS db;
        string strConnection;
        public SourceSelection()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(SourceSelection_Loaded);
        }

        void SourceSelection_Loaded(object sender, RoutedEventArgs e)
        {
            strConnection = CommonCls.BuilderSqlConnectionString(true, @"BILAI-PC\SQLEXPRESS", "VicMessenger", "", "");
            db = new SqlServerFuncKS(strConnection);
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CustomPlaces.Add(FileDialogCustomPlaces.LocalApplicationData);
            dialog.CustomPlaces.Add(FileDialogCustomPlaces.Templates);
            dialog.DefaultExt = ".csv";
            dialog.Filter = "CSV (.csv)|*.csv|Excel Worksheets (*.xls)|*.xls;*.xlsx";
            bool? result = dialog.ShowDialog();

            if (result == true)
            {
                txtFile.Text = dialog.FileName.Trim();



                //var retHeaderInfo = CsvReadFuncKS.CsvHeaderToDataTable(dialog.FileName.Trim());
                //var dd = "";

                //if (retHeaderInfo != null)
                //{
                //    List<proc_CustomeFieldTemplate_SelectResult> modCustTemplateCol = new List<proc_CustomeFieldTemplate_SelectResult>();
                //    foreach (string item in retHeaderInfo)
                //    {
                //        proc_CustomeFieldTemplate_SelectResult modCustTemplate = new proc_CustomeFieldTemplate_SelectResult();
                //        modCustTemplate.FieldName = item;
                //        modCustTemplateCol.Add(modCustTemplate);
                //    }
                //    //lstFieldList.View = CreateGridViewColumns();
                //    //lstFieldList.ItemsSource = PopulateCombobox();
                //}


                lstFields.ItemsSource = PopulateCombobox(CsvReadFuncKS.CsvHeaderToDataTable(txtFile.Text.Trim()));





                //ProcessContact(globalDt);

                //int? prmTotal;
                //var ret = ContactBLL.GetContactCustomPaging(1, 0,100, out prmTotal);

                //lstContact.View = CreateGridViewColumns<proc_Contact_CustomPagingResult>(ret);
                //lstContact.ItemsSource = ret; 

                //globalDt = null;
                //int? prmTotal;
                //globalDt = ContactBLL.GetHorizontalContactCustomPaging(1, 0, 1, out prmTotal);
                //lstContact.View = CreateGridViewColumns(globalDt);
                //lstContact.ItemsSource = globalDt.DefaultView;

                //PagingCtl.TotalPages = (int)prmTotal;


            }
        }


        public List<proc_CustomeFieldTemplate_SelectResult> PopulateCombobox(List<string> prm)
        {

            if (prm != null)
            {
                List<proc_CustomeFieldTemplate_SelectResult> modCustTemplateCol = new List<proc_CustomeFieldTemplate_SelectResult>();
                foreach (string item in prm)
                {
                    proc_CustomeFieldTemplate_SelectResult modCustTemplate = new proc_CustomeFieldTemplate_SelectResult();
                    modCustTemplate.FieldName = item;
                    modCustTemplateCol.Add(modCustTemplate);
                }
                return modCustTemplateCol;
            }
            return null;
        }

        public List<proc_CustomeFieldTemplate_SelectResult> PopulateCombobox(string[] prm)
        {

            if (prm != null)
            {
                List<proc_CustomeFieldTemplate_SelectResult> modCustTemplateCol = new List<proc_CustomeFieldTemplate_SelectResult>();
                foreach (string item in prm)
                {
                    proc_CustomeFieldTemplate_SelectResult modCustTemplate = new proc_CustomeFieldTemplate_SelectResult();
                    modCustTemplate.FieldName = item;
                    modCustTemplateCol.Add(modCustTemplate);
                }
                return modCustTemplateCol;
            }
            return null;
        }

        public GridView CreateGridViewColumns()
        {
            // Create the GridView
            GridView gv = new GridView();
            gv.AllowsColumnReorder = true;


            GridViewColumn gvc = new GridViewColumn();
            gvc.Header = "Standard Field Name";

            FrameworkElementFactory lbl = new FrameworkElementFactory(typeof(TextBlock));
            lbl.SetValue(TextBlock.MinWidthProperty, (double)200);
            Binding bnd = new Binding("FieldName");
            bnd.Mode = BindingMode.OneWay;
            lbl.SetBinding(TextBlock.TextProperty, bnd); // sets binding

            // add textblock template
            gvc.CellTemplate = new DataTemplate();
            gvc.CellTemplate.VisualTree = lbl;
            gv.Columns.Add(gvc);


            return gv;
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {

            List<string> strAllDatabases = db.GetServerDatabases();

            if (strAllDatabases != null)
            {
                lstDatabase.ItemsSource = strAllDatabases;
                lstDatabase.SelectedIndex = 0;

            }




        }

        private void lstDatabase_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstDatabase.SelectedValue != null)
            {
                string selVal = lstDatabase.SelectedValue.ToString();

                var ret = db.GetDatabaseTables(selVal);

                
                if (ret != null)
                {
                    comTables.ItemsSource = null;
                    comTables.Items.Clear();
                    lstFields.ItemsSource = null;
                    lstFields.Items.Clear();
                    if (ret.Count > 0)
                    {
                        comTables.ItemsSource = ret;
                        comTables.SelectedIndex = 0;
                    }

                }


            }

        }

        private void comTables_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comTables.SelectedValue != null)
            {
                string selVal = comTables.SelectedValue.ToString();


                var arr = db.GetTableFields(lstDatabase.SelectedValue.ToString(),
                                                         comTables.SelectedValue.ToString());
                lstFields.ItemsSource = PopulateCombobox(arr);
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < lstFields.Items.Count; i++)
            {
                DependencyObject obj = lstFields.ItemContainerGenerator.ContainerFromIndex(i);
                CheckBox chk = CommonCls.FindVisualChild<CheckBox>(obj);
                if (chk.IsChecked == true)
                {
                    TextBlock tb = CommonCls.FindVisualChild<TextBlock>(obj);

                    proc_CustomeFieldTemplate_SelectResult mod = new proc_CustomeFieldTemplate_SelectResult();
                    mod.CustomeFieldTemplateID = 0;
                    mod.CampaignID = 10;
                    mod.FieldName = tb.Text.Trim();
                    mod.RealFieldName = string.Format("#{0}#", tb.Text.Trim().ToLower().Replace(" ", ""));
                    mod.Active = 1;

                    CustomFieldTemplateBLL.InsertUpdateCustomFieldTemplate(mod);
                }
                //TextBlock box = CommonCls.FindVisualChild<TextBlock>(obj);

            }
        }


    }

}
